package edu.vt.cbil.ui;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SampleEditForm extends JDialog {
    private JPanel contentPane;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton setButton;
    private JButton setButton1;
    private JButton clearButton;
    private JButton addButton;
    private BACOMGUI p_mainForm;
    private String samplePath;

    public SampleEditForm(BACOMGUI mForm) {
        setContentPane(contentPane);
        setModal(true);

        p_mainForm = mForm;

        clearButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                textField1.setText("");
                textField2.setText("");
                textField3.setText("");
            }
        });


        addButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                if ((textField1.getText().length() == 0) ||
                        (textField2.getText().length() == 0) ||
                        (textField3.getText().length() == 0)) {
                    new JOptionPane().showMessageDialog(null, "Field can't be empty!");
                } else {
                    p_mainForm.addNewSamplePair(textField1.getText(), textField2.getText(), textField3.getText());
                }
            }
        });


        setButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "SNP 6.0 CEL File (*.cel)", "cel");
                chooser.setFileFilter(filter);
                if (samplePath.length() > 0) {
                    chooser.setCurrentDirectory(new File(samplePath));
                }

                int returnVal = chooser.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    String selectFile = chooser.getSelectedFile().getName();
                    textField2.setText(selectFile);
                }
            }
        });
        setButton1.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "SNP 6.0 CEL File (*.cel)", "cel");
                chooser.setFileFilter(filter);
                if (samplePath.length() > 0) {
                    chooser.setCurrentDirectory(new File(samplePath));
                }

                int returnVal = chooser.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    String selectFile = chooser.getSelectedFile().getName();
                    textField3.setText(selectFile);
                }

            }
        });
    }

    public void setSamplePath(String samplePath) {
        this.samplePath = samplePath;
    }
}
